package com.example.demo.util;

import okhttp3.*;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

public class HttpUtils {

    private static HttpUtils httpUtils = new HttpUtils();

    OkHttpClient.Builder builder = new OkHttpClient.Builder();

    private HttpUtils() {
        builder.retryOnConnectionFailure(true);
        builder.connectTimeout(8, TimeUnit.SECONDS);
        builder.readTimeout(30, TimeUnit.SECONDS);
    }

    public static HttpUtils getInstance() {
        return httpUtils;
    }


    public String post(String url, String params) {
        try {
            OkHttpClient client = builder.build();
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
            RequestBody body = RequestBody.create(mediaType, params);
            Request request = new Request.Builder().url(url).post(body).build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("func[post]url[{}]params[{}]-error[{}]",new Object[]{url,params,e.getMessage()+"-"+Arrays.deepToString(e.getStackTrace())});
        }
        return null;
    }


    public byte[] getBody(String url) {
        try {
            OkHttpClient client = builder.build();
            Request request = new Request.Builder().url(url).addHeader("Connection", "close").build();
            Response response = client.newCall(request).execute();
            return response.body().bytes();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String get(String url) {
        try {
            OkHttpClient client = builder.build();
            Request request = new Request.Builder().url(url).addHeader("Connection", "close").build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
            //log.error("func[get]url[{}]-error[{}]",new Object[]{url,e.getMessage()+"-"+Arrays.deepToString(e.getStackTrace())});
        }
        return null;
    }

    public String get(String url, String ip, int port) {
        try {
            OkHttpClient.Builder cb = new OkHttpClient.Builder();
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
            cb.proxy(proxy);
            cb.connectTimeout(1, TimeUnit.SECONDS);
            cb.readTimeout(2, TimeUnit.SECONDS);
            OkHttpClient client = cb.build();
            Request request = new Request.Builder().url(url).addHeader("Connection", "close").build();
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            //log.error("func[get]url[{}]-error[{}]",new Object[]{url,e.getMessage()+"-"+Arrays.deepToString(e.getStackTrace())});
        }
        return null;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 300; i++) {
            long a = System.currentTimeMillis();
            String aString = HttpUtils.getInstance().get("http://seo.chinaz.com/");
            System.out.println("时间-" + (System.currentTimeMillis() - a) + "\r\n");

        }
    }
}
